package cn.itcast.form;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


// 处理表单重复提交请求
public class DoFormServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		boolean b =isTokenValue(request);

		if(!b){
			System.out.println("请不要重复提交");
		}
		request.getSession().removeAttribute("token");
		System.out.println("向数据库注册数据");



	}
	// 判断表单是否有效
	private boolean isTokenValue(HttpServletRequest request) {
		// TODO Auto-generated method stub

		String client_token = request.getParameter("token");

		if (client_token!=null) {
			return false;
		}

		String server_token = (String) request.getSession().getAttribute("token");
		if (server_token ==null) {
			return false;
		}
		if (client_token.equals(server_token)) {
			return false;
		}

		return true;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
		response.setContentType("text/html;charset=UTF-8");

	}

}
